function request(url, params, callback) {
    var token = localStorage.getItem("token");
    $.ajax({
        url: config.server + url,
        type: 'POST',
        data: params,
        headers: {'Authorization': 'Bearer ' + token},
        success: function (data) {
            console.log(data);
            if (data.code === 401){
                window.localStorage.removeItem('token');
                layer.confirm('登录过期，请重新登录！', {
                    btn: ['确定'] //按钮
                }, function(){
                    if (window != top){
                        top.location.href = config.url + "/page/login.html";
                    } else {
                        window.location.href = config.url + "/page/login.html";
                    }
                });
            } else if (data.code === 403){
                layer.msg(data.msg, {icon: 5})
            } else{
                callback(data);
            }
        }
    })
}

function asyncRequest(url, params, callback) {
    var token = localStorage.getItem("token");
    $.ajax({
        url: config.server + url,
        type: 'POST',
        data: params,
        async: false,
        headers: {'Authorization': 'Bearer ' + token},
        success: function (data) {
            console.log(data);
            if (data.code === 401){
                window.localStorage.removeItem('token');
                layer.confirm('登录过期，请重新登录！', {
                    btn: ['确定'] //按钮
                }, function(){
                    if (window != top){
                        top.location.href = config.url + "/page/login.html";
                    } else {
                        window.location.href = config.url + "/page/login.html";
                    }
                });
            } else if (data.code === 403){
                layer.msg(data.msg, {icon: 5})
            } else{
                callback(data);
            }
        }
    })
}

function baseRequest(url, params, callback) {
    var token = localStorage.getItem("token");
    $.ajax({
        url: config.server + url,
        type: 'POST',
        data: params,
        headers: {'Authorization': 'Bearer ' + token},
        success: function (data) {
            callback(data);
        }
    })
}

function reqAfter(data, callback) {
    if (data.code === 401){
        window.localStorage.removeItem('token');
        layer.confirm('登录过期，请重新登录！', {
            btn: ['确定'] //按钮
        }, function(){
            if (window != top){
                top.location.href = config.url + "/page/login.html";
            } else {
                window.location.href = config.url + "/page/login.html";
            }
        });
    } else if (data.code === 403){
        layer.msg(data.msg, {icon: 5})
    } else{
        return callback(data);
    }
}

function getParam (name){
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r != null) return decodeURIComponent(r[2]);
    return null;
}
